import React from "react";
import { Form, Input, Button,message } from 'antd'
import './login.css'
import { getMenu } from '../../api/index'
import { useNavigate,Navigate } from "react-router-dom";
const Login = () => {
  const navigate = useNavigate();
  // 在登录状态下，需要跳转到home页
  if (localStorage.getItem('token')) {
    return <Navigate to="/home" replace />
  }
  const handleSubmit = (val) => {
    if (!val.username || !val.password) {
      return message.open({
        type: 'error',
        content: '账号和密码不能为空'
      })
    } else {
      getMenu(val).then((data) => {
        console.log(data);
        
          // 存储 token
          localStorage.setItem('token', data.data.token)
        // 跳转到首页
        navigate('/')
      }).catch((err) => {
        // message.error('登录失败，请重试')
      })
    }
  }
  return (
    <Form className="login-container" onFinish={handleSubmit}>
      <div className="login_title">系统登录</div>
      <Form.Item label="账号" name="username">
        <Input placeholder="请输入账号" />
      </Form.Item>
      <Form.Item label="密码" name="password">
        <Input.Password placeholder="请输入账号" />
      </Form.Item>
      <Form.Item className="login-button">
        <Button type="primary" htmlType="submit">
          登录
        </Button>
      </Form.Item>
    </Form>
  )
}
export default Login; 